package com.escool.controller;

import java.util.Map;

import javax.validation.Valid;


import org.springframework.beans.factory.annotation.Autowired;
import com.escool.model.TbPeriodo;
import com.escool.service.PeriodoService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class PeriodoController {
	
	@Autowired
	PeriodoService periodoService;
	
	@RequestMapping("showPeriodo")
	public String listPeriodo(Map<String, Object> map){
		map.put("periodo", new TbPeriodo());
		map.put("periodoList", periodoService.listPeriodo());
		return "/periodo";
	}
	
	@RequestMapping(value="addPeriodo",method= RequestMethod.POST)
	public String addPeriodo(@ModelAttribute("periodo") @Valid TbPeriodo periodo, BindingResult result){
		if(!result.hasErrors()){
			periodoService.addPeriodo(periodo);
			return "redirect:/showPeriodo";
		}
		return "";
	}
	
	@RequestMapping(value="editPeriodo",method= RequestMethod.POST)
	public String editPeriodo(@ModelAttribute("periodo") @Valid TbPeriodo periodo, BindingResult result){
		if(!result.hasErrors()){
			periodoService.updatePeriodo(periodo);
			return "redirect:/showPeriodo";
		}
		return "";
	}

	@RequestMapping(value="deletePeriodo",method= RequestMethod.POST)
	public String deletePeriodo(@ModelAttribute("periodo") @Valid TbPeriodo periodo, BindingResult result){
		if(!result.hasErrors()){
			periodoService.removePeriodo(periodo.getIdPeriodo());
			return "redirect:/showPeriodo";
		}
		return "";
	}
}
